<?php

// $Id$


require_once(TANGRA_MAIN_DIR.'interfaces/i_db_storable.class.php');

require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/settings/tcms_settings_attachments_dbc.class.php');
require_once('tcms_attachment.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/ci/tcms_ci_attachment_dbc.class.php');


class TCMS_Attachment_DBC extends TCMS_Attachment implements I_DB_Storable {

	public function save(DB_Connection $dbc) {
		$ret = false;

		if ($this->get_id()) {
			$ret = $this->update($dbc);
		} else {
			$ret = $this->insert($dbc);
			$this->set_id($ret);
		}

		return $ret;
	}


	public function load_by_id(DB_Connection $dbc, $id) {
		tangra_if_not_int_throw_e($id);

		$ret = false;

		$sql = "select orig_filename, "
						."size, "
						."path, "
						."filename, "
						."extension, "
						."note "
					."from tcms_attachments where id = $id";

		$rez = $dbc->execute($sql);

		if (!$rez->is_eof()) {
			$rez_obj = $rez->fetch_object();

			$this->set_id($id);
			$this->set_orig_filename(stripslashes($rez_obj->ORIG_FILENAME));
			$this->set_size($rez_obj->SIZE);
			$this->set_path(stripslashes($rez_obj->PATH));
			$this->set_filename(stripslashes($rez_obj->FILENAME));
			$this->set_extension(stripslashes($rez_obj->EXTENSION));
			$this->set_note(stripslashes($rez_obj->NOTE));


			$ret = $id;
		}

		return $ret;
	}


	protected function insert(DB_Connection $dbc) {
		$ret = false;

		$id = $dbc->generate_id('tcms_attachments_seq');

		$orig_filename = addslashes($this->get_orig_filename());
		$size = $this->get_size();
		$path = addslashes($this->get_path());
		$filename = addslashes($this->get_filename());
		$extension = addslashes($this->get_extension());
		$note = addslashes($this->get_note());

		if ($id) {
			$sql = "insert into tcms_attachments (id, ".
									      "orig_filename, ".
									      "size, ".
									      "path, ".
									      "filename, ".
									      "extension, ".
									      "note ".
									      ") ".
								"values ".
									      "($id,".
									      "'$orig_filename', ".
									      "$size, ".
									      "'$path', ".
									      "'$filename', ".
									      "'$extension', ".
									      "'$note' ".
									      ")";
			$dbc->execute($sql);
			$ret = $id;
		} else {
			throw new TE_Exception('tcms_attachments_seq');
		}

		return $ret;
	}


	protected function update(DB_Connection $dbc) {
		$id = $this->get_id();

		$orig_filename = addslashes($this->get_orig_filename());
		$size = $this->get_size();
		$path = addslashes($this->get_path());
		$filename = addslashes($this->get_filename());
		$extension = addslashes($this->get_extension());
		$note = addslashes($this->get_note());

		$sql = "update tcms_attachments set ".
						"orig_filename = '$orig_filename', ".
						"size = $size, ".
						"path = '$path', ".
						"filename = '$filename', ".
						"extension = '$extension', ".
						"note = '$note' ".
					"where id = ".$id;

		$dbc->execute($sql);
		$ret = $id;

		return $ret;
	}


	public static function delete(DB_Connection $dbc, $id) {
		tangra_if_not_int_throw_e($id);

		$sql = "delete from tcms_attachments where id = $id";
		$dbc->execute($sql);
	}


	public static function delete_cascade(DB_Connection $dbc, $id) {
		TCMS_Attachment_DBC::delete($dbc, $id);
		TCMS_ci_Attachment_DBC::delete_for_attachment($dbc, $id);
	}


	public static function get_sql_for_grid($filename, $note) {
		$where = self::prepare_where_clause($filename, $note);
		$sql = "select id, orig_filename, size, note from tcms_attachments a ".$where." order by id desc";

		return $sql;
	}


	public static function get_sql_count_for_grid($filename, $note) {
		$where = self::prepare_where_clause($filename, $note);
		$sql = "select count(id) as total_rows from tcms_attachments a ".$where;

		return $sql;
	}


	private static function prepare_where_clause($filename, $note) {
		$filename = self::remove_duplicate_asteriks($filename);
		$note = self::remove_duplicate_asteriks($note);

		if ($filename || $note) {
			$where = '';
			if ($filename) {
				if (strpos($filename, '*') !== false) {
					$filename = addslashes($filename);
					$filename = str_replace('*', '%', $filename);
					$where .= "a.orig_filename like '$filename' ";
				} else {
					$filename = addslashes($filename);
					$where .= "a.orig_filename = '$filename' ";
				}
			}

			if ($note) {
				if (strpos($note, '*') !== false) {
					$note = addslashes($note);
					$note = str_replace('*', '%', $note);
					if ($where) {
						$where .= "and ";
					}
					$where .= "(a.note like '$note')";
				} else {
					if ($where) {
						$where .= "and ";
					}
					$note = addslashes($note);
					$where .= "a.note = '$note'";
				}
			}
		} else {
			$where = '';
		}

		if ($where) {
			$where = " where ".$where;
		}

		return $where;
	}


	private static function remove_duplicate_asteriks($filter) {
		$ret = $filter;
		while(strpos($ret, '**') !== false) {
			$ret = str_replace('**', '*', $ret);
		}

		return $ret;
	}
}
